'''
Created on Oct 19, 2010

@author: riche
'''
import unittest
import filecmp
from ft.framework import connector, fake_endp
from ft.directory import localhost

class Test(unittest.TestCase):


    def test_instrument(self):
        conn = connector.InstrumentedConnector(fake_endp.generate_empty_endpoint(), fake_endp.generate_empty_endpoint())
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint('{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root))
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')
        
    def test_conversion(self):
        conn = connector.Connector(fake_endp.generate_empty_endpoint(), fake_endp.generate_empty_endpoint())
        conn = conn.instrument('{}ft/tests/framework/'.format(localhost.local_directory.package_root))
        self.assertEqual(conn._filename, '{}ft/tests/framework/i_FAKE-FAKE_o_FAKE-FAKE.txt'.format(localhost.local_directory.package_root))
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint()
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/i_FAKE-FAKE_o_FAKE-FAKE.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')

    def test_file_init(self):
        conn = connector.InstrumentedConnector(fake_endp.generate_empty_endpoint(), 
                                               fake_endp.generate_empty_endpoint(), 
                                               '{}ft/tests/framework/CONN_INIT_OUT.txt'.format(localhost.local_directory.package_root))
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint()
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/CONN_INIT_OUT.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')

    def test_file_override(self):
        conn = connector.InstrumentedConnector(fake_endp.generate_empty_endpoint(), 
                                               fake_endp.generate_empty_endpoint(), 
                                               '{}ft/tests/framework/CONN_INIT_OUT.txt'.format(localhost.local_directory.package_root))
        infile = open('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root), 'r')
        for msg in infile:
            conn.put(msg)
        conn.checkpoint('{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root))
        ret = filecmp.cmp('{}ft/tests/framework/TEST_INPUT.txt'.format(localhost.local_directory.package_root),
                          '{}ft/tests/framework/CONN_OUT.txt'.format(localhost.local_directory.package_root),
                          False)
        self.assertEqual(ret, True, 'Files not equal')

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_instrument']
    unittest.main()